﻿<%@ Page Language="C#" %>

<%@ Import Namespace="System.Globalization" %>

<script runat="server">

    void Page_Load()
    {
        if (!IsPostBack)
        {
            BindShoppingCart();
        }
    }

    void BindShoppingCart()
    {
        if (Profile.ShoppingCart != null)
        {
            CartGrid.DataSource = Profile.ShoppingCart.CartItems;
            CartGrid.DataBind();
            lblTotal.Text = Profile.ShoppingCart.Total.ToString("c");
        }
    }

    void AddCartItem(Object s, EventArgs e)
    {
        GridViewRow row = ProductGrid.SelectedRow;

        int ID = (int)ProductGrid.SelectedDataKey.Value;
        String Name = row.Cells[1].Text;
        decimal Price = Decimal.Parse(row.Cells[2].Text,
          NumberStyles.Currency);

        if (Profile.ShoppingCart == null)
            Profile.ShoppingCart = new ShoppingCart();

        Profile.ShoppingCart.AddItem(ID, Name, Price);
        BindShoppingCart();
    }

    void RemoveCartItem(Object s, EventArgs e)
    {
        int ID = (int)CartGrid.SelectedDataKey.Value;
        Profile.ShoppingCart.RemoveItem(ID);
        BindShoppingCart();
    }
</script>

<html>
<head>
    <title>Products</title>
</head>
<body>
    <form id="form1" runat="server">
    <table width="100%">
        <tr>
            <td valign="top">
                <h2>
                    Products</h2>
                <asp:GridView ID="ProductGrid" DataSourceID="ProductSource" DataKeyNames="ProductID"
                    AutoGenerateColumns="false" OnSelectedIndexChanged="AddCartItem" ShowHeader="false"
                    CellPadding="5" runat="Server">
                    <Columns>
                        <asp:ButtonField CommandName="select" Text="Buy" />
                        <asp:BoundField DataField="ProductName" />
                        <asp:BoundField DataField="UnitPrice" DataFormatString="{0:c}" />
                    </Columns>
                </asp:GridView>
                <asp:SqlDataSource ID="ProductSource" ConnectionString="Server=localhost;Database=Northwind;Trusted_Connection=true;"
                    SelectCommand="SELECT ProductID,ProductName,UnitPrice FROM Products" runat="Server" />
            </td>
            <td valign="top">
                <h2>
                    Shopping Cart</h2>
                <asp:GridView ID="CartGrid" AutoGenerateColumns="false" DataKeyNames="ID" OnSelectedIndexChanged="RemoveCartItem"
                    CellPadding="5" Width="300" runat="Server">
                    <Columns>
                        <asp:ButtonField CommandName="select" Text="Remove" />
                        <asp:BoundField DataField="Name" HeaderText="Name" />
                        <asp:BoundField DataField="Price" HeaderText="Price" DataFormatString="{0:c}" />
                        <asp:BoundField DataField="Quantity" HeaderText="Quantity" />
                    </Columns>
                </asp:GridView>
                <b>Total:</b>
                <asp:Label ID="lblTotal" runat="Server" />
            </td>
        </tr>
    </table>
    </form>
</body>
</html>
